package com.json.smtest.controller;

import com.json.smtest.SMUtils.SMEnAndEdcodeUtils;
import com.json.smtest.bean.ParamBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class SMController {

    @PostMapping(value = "/sm2/decode",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Object sm2Decode(@RequestBody ParamBean paramBean){
        //04a6e1c02facc8904551ad686fad5e9382211b318e832e642d342ef3d7623ddc21833efdc81fec74469a7faf38fab54efb7135b6bff4970e1e77c499e9754abdb90c120b2fbce28e2e00607425f36513a65e06881bc83f0a3560d5360f94b25e42c3e3954b6ac557d98442aabba0faf0ed2d642fc6f5941b073d8a022a
        String text = paramBean.getText();
        String out = SMEnAndEdcodeUtils.decrypt_sm2(text);
        log.info("原始数据{}，sm2解密后数据{}", text,out);
        Map<String,Object> map = new HashMap<>();
        map.put("out",out);
        return map;
    }

    @PostMapping(value = "/sm2/encode",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Object sm2encode(@RequestBody ParamBean paramBean){

        String text = paramBean.getText();
        String out = SMEnAndEdcodeUtils.encrypt_sm2(text);
        log.info("原始数据{}，sm2解密后数据{}", text,out);
        Map<String,Object> map = new HashMap<>();
        map.put("out",out);
        return map;
    }

    @PostMapping(value = "/sm4/decode",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Object sm4Decode(@RequestBody ParamBean paramBean){

        String text = paramBean.getText();
        String out = SMEnAndEdcodeUtils.decrypt_sm4_cbc(text);
        log.info("原始数据{}，sm4解密后数据{}", text,out);
        Map<String,Object> map = new HashMap<>();


        map.put("out",SMEnAndEdcodeUtils.encrypt_sm4_cbc(out));
        return map;
    }

    @PostMapping(value = "/sm4/encode",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Object sm4encode(@RequestBody ParamBean paramBean){

        String text = paramBean.getText();
        String out = SMEnAndEdcodeUtils.encrypt_sm4_cbc(text);
        log.info("原始数据{}，sm4加密后数据{}", text,out);
        Map<String,Object> map = new HashMap<>();
        map.put("out",out);
        return map;
    }


}
